package com.blog.controller.forntend;

import com.baomidou.mybatisplus.plugins.Page;
import com.blog.entity.Article;
import com.blog.entity.Product;
import com.blog.service.ArticleService;
import com.blog.service.CertService;
import com.blog.service.ProductService;
import com.blog.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/frontend/product")
@Controller
public class ProductController extends BaseController {

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CertService certService;

    @RequestMapping("/index")
    public String index(Model model, Integer curPage) {
        model.addAttribute("types", productTypeService.getTypes());
        model.addAttribute("page", productService.index(null, new Page<Product>(curPage == null ? 1 : curPage, 5)));
        model.addAttribute("articles", articleService.index(null, new Page<Article>(curPage == null ? 1 : curPage, 5)));
        return "frontend/product/index";
    }


    /**
     * 根据分类显示
     *
     * @return
     */
    @RequestMapping("/show")
    public String show(Model model, Integer type, Integer curPage) {
        model.addAttribute("types", productTypeService.getTypes());
        Product product = new Product();
        if (type != null) {
            model.addAttribute("curType", productTypeService.view(type));
            product.setType(type);
            model.addAttribute("page", productService.index(product, new Page<Product>(curPage == null ? 1 : curPage, 5)));
        } else {
            model.addAttribute("page", productService.index(null, new Page<Product>(curPage == null ? 1 : curPage, 10)));
        }
        return "frontend/product/show";
    }

    @RequestMapping("/view")
    public String view(Model model, Integer id) {
        model.addAttribute("types", productTypeService.getTypes());
        model.addAttribute("product", productService.view(id));
        return "frontend/product/view";
    }

}